<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends CI_Controller {

	public $data;

	public function __construct()
	{
		parent::__construct();
		//载入模型
		//$this->load->model('User_model');
		$this->load->model('Login_model');
		//载入库
		$this->load->library('session');
		$this->load->library('Forbid_resubmit');
		//初始化视图变量
		$data = array();
		$this->data['site'] = $this->config->item('site');
		$this->data['menus'] = $this->config->item('menus');
		$this->data['styles'] = array();
		$this->data['scripts'] = array('master.js');
		$this->data['title'] = '系统登录';
	}

	public function index()
	{
		//首先自动登出
		$this->Login_model->logout();

		if ($this->Login_model->check())
		{
			//登录成功，赋值
			$user = $this->Login_model->user();
			$this->data['header']['username'] = $user['realname'];
			$this->data['site']['environment'] = $user['environment'];
			$this->data['menus'] = $this->config->item('menus');
			$this->data['continue'] = $this->data['menus'][0]['url'];
			$this->load->view_('login_success_view', $this->data);
			return;
		}
		else
		{
			$this->data['form']['error'] = $this->Login_model->error;
		}
		//验证失败或重复提交
		$this->data['form']['rand'] = $this->forbid_resubmit->set_submit_rand();
		$this->load->view_('login_view', $this->data);
		return FALSE;
	}

	public function logout()
	{
		$this->Login_model->logout();
		//验证失败或重复提交
		$this->data['form']['rand'] = $this->forbid_resubmit->set_submit_rand();
		$this->load->view_('login_view', $this->data);
		return FALSE;
	}

}

/* End of file login.php */